/**
 * 
 */
package com.chengyu.eyc.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;
import org.jxls.util.TransformerFactory;

public final class ExcelUtils {


   
    public static void writeExcel(InputStream is, List<?> beans, OutputStream os) throws IOException {
        writeExcel(is, beans, os, null);
    }

    public static void writeExcel(InputStream is, List<?> list, OutputStream os, Map<String, Object> functionMap)
        throws IOException {
        Transformer transformer = TransformerFactory.createTransformer(is, os);
        JexlExpressionEvaluator evaluator =
            (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
        evaluator.getJexlEngine().setSilent(true);
        if (functionMap != null) {
            evaluator.getJexlEngine().setFunctions(functionMap);
        }
        Context context = new Context();
        context.putVar("list", list);
        JxlsHelper.getInstance().processTemplate(context, transformer);
    }

    public static void writeExcel(InputStream is, Map<String, Object> map, OutputStream os,
        Map<String, Object> functionMap) throws IOException {
        Transformer transformer = TransformerFactory.createTransformer(is, os);
        JexlExpressionEvaluator evaluator =
            (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
        evaluator.getJexlEngine().setSilent(true);
        if (functionMap != null) {
            evaluator.getJexlEngine().setFunctions(functionMap);
        }
        Context context = new Context();
        for (Entry<String, Object> entry : map.entrySet()) {
            context.putVar(entry.getKey(), entry.getValue());
        }

        JxlsHelper.getInstance().processTemplate(context, transformer);
    }

  

  

    private ExcelUtils() {

    }

}
